import { _decorator, Collider2D,Animation, Component, Contact2DType, Node } from 'cc';
import { GameManager, GameState } from '../GameManager';
const { ccclass, property } = _decorator;

@ccclass('Bullet_Player_2')
export class Bullet_Player_2 extends Component {

    @property
    speed:number=600;

    @property(Node)
    bombRange:Node=null;
    @property
    bombSpeed:number=300;
    isBomb:boolean=false;

    isMoving:boolean=true;
    
    bulletCollider: Collider2D = null;

    @property(Animation)
    anim: Animation = null;

    start() {
        this.bulletCollider=this.node.getComponent(Collider2D);
        this.bulletCollider.on(Contact2DType.BEGIN_CONTACT,()=>{
            this.isMoving=false;
            this.isBomb=true;
            this.bulletCollider.off(Contact2DType.BEGIN_CONTACT);
        })

    }

    update(deltaTime: number) {
        this.moving(deltaTime);
        this.bomb(deltaTime);
        if(this.node.position.y>=1200){
            this.node.destroy();
        }
    }

    moving(deltaTime){
        if (GameManager.getInstance().gameState == GameState.START){
                
            if(this.isMoving){
                const newPos=this.node.getPosition();
                newPos.y+=this.speed*deltaTime;
                this.node.setPosition(newPos.x,newPos.y);
            }
        }
    }
    bomb(deltaTime){
        if(this.isBomb){
            this.anim.play("EnemyDead");
            // console.log("bomb");
            const bombSc=this.bombRange.getScale();

            bombSc.x+=this.bombSpeed*deltaTime;
            bombSc.y+=this.bombSpeed*deltaTime;

            this.bombRange.setScale(bombSc);

            if(bombSc.x>=40){
                this.isBomb=false;
                setTimeout(() => {
                    this.node.destroy();
                }, 200);
            }
        }
    }
}


